﻿using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading.Tasks;
using WebApiOrm.Domain;

namespace WebApiOrm.Provider.FreeSqlSqlite
{
    sealed class FreeSqlOrmProvider : IOrmProvider
    {
        private readonly IFreeSql fsql;

        public Version? Version => fsql.GetType().Assembly.GetName().Version;

        public FreeSqlOrmProvider(IFreeSql fsql)
        {
            this.fsql = fsql;
        }

        public async Task<bool> AddSysUserAsync(SysUser user)
        {
            var count = await fsql.Insert(user).ExecuteAffrowsAsync();
            return count > 0;
        }

        public async Task<IList<SysUser>> QuerySysUsersAsync(Expression<Func<SysUser, bool>> predicate, int skip, int take)
        {
            return await fsql.Select<SysUser>().Where(predicate).OrderBy(item => item.Id).Skip(skip).Take(take).ToListAsync();
        }
    }
}